/*card type
 * */
var CARD_TYPE = {
    //1~999         direct  effect card
    ATTACK_CARD:1,             //norma attack
    SPECAIL_ATTACK_CARD:2,    //special attack
    HP_RECOVER_CARD:3,        //hp recover
    CANCEL_BUFFER_CARD:4,   //cancel enemy's buffer
    SWAP_BUFFER_CARD:5,      //swap buffer

    //1001~1999     buffer card
    ATTACK_BUFFER_CARD:1000,    //buffer add  attack
    DEFENSE_BUFFER_CARD:1001,   //buffer add defense
    HP_RECOVER_BUFFER_CARD:1002,//buffer recover hp each round
    JUDGE_BUFFER_CARD:1003,       //plus judge number
    REDUCE_CARD_BUFFER_CARD:1004,   //buffer reduce enemy's card number

    //2000~3000   hold effect card
    DODGE_CARD:1005,
    COUNTER_CARD:1006,
    DODGE_COUNTER_CARD:1007,
    RELIVE_CARD:1008,
};
//normal attack card

var Card = function(){
    function Card(name,type,params){
        this.name = name;
        this.type = type;
        this.params = params;
    }
    //utility functions

    function getCardTypeByName(name){
        return app.getConfig().cards[name].type;
    }

    function getPlayCardsTypeByName(cards){
        return getCardTypeByName[cards[0]];
    }

    function getPlayCardsAction(cards){
        return ACTION_TYPE.ATTACK;
    }

    function isDirectEffectCard(type){
        return type >= 1 && type <=999;
    }

    function isBufferEffectCard(type){
        return type >=1000 && type <=1999;
    }

    function isHoldEffectCard(type){
        return type >=2000 && type <=2999;
    }

    function getCombo(cards){
        var hash = "";
        for(var i = 0;i < cards.length; ++i){
            if(cards[i].type !== CARD_TYPE.ATTACK_CARD){
                return null;
            }
            hash += cards[i].num;
        }
        if(app.getConfig().combo.hasOwnProperty(hash)){
            return [hash,app.getConfig().combo[hash]];
        }
        return null;
    }

    function createComboCard(cards){
        var t = getCombo(cards);
        if(t){
            var comboName = t[0];
            var combo = t[1];
            return new Card(comboName,combo.type,combo.params);
        }else{
            return null;
        }
    }

    function createEffectCard(cards){
        if(cards[0].type === CARD_TYPE.ATTACK_CARD){
            var combo = createComboCard(cards);
            if(combo){
                return combo;
            }
            var totalNum = 0;
            for(var i = 0; i < cards.length; ++i){
                if(cards[i].type === CARD_TYPE.ATTACK_CARD){
                    totalNum += cards[i].params["num"];
                }else{
                    return null;
                }
            }
            return new Card("attack",CARD_TYPE.ATTACK_CARD,{"num":totalNum});
        }else{
            return cards[0];
        }
    }

    function createCardByName(name,params){
        var type =  getCardTypeByName(name);
        return new Card(name,type,params);
    }

    return {
        "getCardTypeByName":getCardTypeByName,
        "getPlayCardsTypeByName":getPlayCardsTypeByName,
        "getPlayCardsAction":getPlayCardsAction,
         "isDirectEffectCard":isDirectEffectCard,
        "isBufferEffectCard":isBufferEffectCard,
        "isHoldEffectCard":isHoldEffectCard,
        "getCombo":getCombo,
         "createCardByName":createCardByName,
        "createEffectCard":createEffectCard,
    }
}

module.exports = {
    "Card":Card(),
    "CARD_TYPE":CARD_TYPE,
};

